﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Pub;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
using System.IO;
using System.Globalization;
using LZQXT.Test.Forms.Script;
using LZQXT.Test;

namespace Script
{
    public partial class FScript : Form
    {
        FCS fcs = new FCS();
        FJS fjs = new FJS();

        public FScript()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            tabControl1.Dock = DockStyle.Fill;

            fcs.TopLevel = false;
            fcs.Parent = tabPage2;
            fcs.Dock = DockStyle.Fill;
            fcs.FormBorderStyle = FormBorderStyle.None;
            fcs.Show();

            fjs.TopLevel = false;
            fjs.Parent = tabPage1;
            fjs.Dock = DockStyle.Fill;
            fjs.FormBorderStyle = FormBorderStyle.None;
            fjs.Show();
        }

        private void btnCs_Click(object sender, EventArgs e)
        {

        }


        public void Compile_Test()
        {
            // 1.CSharpCodePrivoder
            CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();

            // 2.ICodeComplier
            ICodeCompiler objICodeCompiler = objCSharpCodePrivoder.CreateCompiler();

            // 3.CompilerParameters
            CompilerParameters objCompilerParameters = new CompilerParameters();
            string[] s_dll = new string[] { "System.dll", "Pub.dll",
                "System.Xml.dll", "System.Data.dll","System.Web.dll" };

            for (int i = 0; i < s_dll.Length; i++)
                objCompilerParameters.ReferencedAssemblies.Add(s_dll[i]);

            objCompilerParameters.GenerateExecutable = false;
            objCompilerParameters.GenerateInMemory = true;

            // 4.CompilerResults
            CDebug.Send(GenerateCodeII());
            CompilerResults cr = objICodeCompiler.CompileAssemblyFromSource(objCompilerParameters, GenerateCodeII());

            if (cr.Errors.HasErrors)
            {
                //Console.WriteLine("编译错误：");
                MessageBox.Show("编译错误：");
                foreach (CompilerError err in cr.Errors)
                {
                    //Console.WriteLine(err.ErrorText);
                    MessageBox.Show(err.ErrorText);
                }
            }
            else
            {
                /*
                // 通过反射，调用HelloWorld的实例
                Assembly objAssembly = cr.CompiledAssembly;
                object objHelloWorld = objAssembly.CreateInstance("DynamicCodeGenerate.HelloWorld");
                MethodInfo objMI = objHelloWorld.GetType().GetMethod("OutPut");

                //Console.WriteLine(objMI.Invoke(objHelloWorld, null));

                MessageBox.Show(objMI.Invoke(objHelloWorld, null).ToString());
                */


                Assembly objAssembly = cr.CompiledAssembly;
                object objHelloWorld = objAssembly.CreateInstance("Meteorologicalservice.Class1");
                MethodInfo objMI = objHelloWorld.GetType().GetMethod("getTemVal");
                object[] parameters = new object[] { "52889", "20120101", "20120901" };

                MessageBox.Show(objMI.Invoke(objHelloWorld, parameters).ToString());

            }

            Console.ReadLine();
        }

        /*
        static string GenerateCode()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("using System;");
            sb.Append(Environment.NewLine);
            sb.Append("namespace DynamicCodeGenerate");
            sb.Append(Environment.NewLine);
            sb.Append("{");
            sb.Append(Environment.NewLine);
            sb.Append("    public class HelloWorld");
            sb.Append(Environment.NewLine);
            sb.Append("    {");
            sb.Append(Environment.NewLine);
            sb.Append("        public string OutPut()");
            sb.Append(Environment.NewLine);
            sb.Append("        {");
            sb.Append(Environment.NewLine);
            sb.Append("             return \"Hello world!\";");
            sb.Append(Environment.NewLine);
            sb.Append("        }");
            sb.Append(Environment.NewLine);
            sb.Append("    }");
            sb.Append(Environment.NewLine);
            sb.Append("}");

            string code = sb.ToString();
            Console.WriteLine(code);
            Console.WriteLine();

            return code;
        }
        */

        static string GenerateCodeII()
        {
            return CFile.Read(@"D:\Program\Tem_CSharp\兰州市气象局决策与专业气象服务系统\LZQX_WEB\MeteorologicalservicesSystem\code\Class1.cs", Encoding.UTF8);
        }




    }

    public static class JTBOX
    {
        /// <summary>
        /// 注册插件
        /// </summary>
        /// <param name="node1"></param>
        /// <param name="list1"></param>
        /// <returns></returns>
        public static bool RegPlug(List<Form> list1, ImageList imgLst)
        {
            try
            {
                FScript f1 = new FScript();
                list1.Add(f1);

                imgLst.Images[list1.Count - 1] = f1.imageList2.Images[0];
            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
                return false;
            }
            return true;
        }
    }

}
